<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Builder;

trait Common
{

    /**
     * 分页
     * @param Builder $query
     * @param int $page
     * @param int $pageSize
     * @return Builder
     * @Author 姿势就是力量
     */
    public function scopePagination(Builder $query, int $page = 1, int $pageSize = 10): Builder
    {
        return $query->limit($pageSize)->offset(($page - 1) * $pageSize);
    }


    /**
     * 管理员所属公司条件
     * @param Builder $query
     * @param int $companyId 公司ID
     * @return Builder
     * @Author 姿势就是力量int
     */
    public function scopeCompany(Builder $query, $companyId = null): Builder
    {
        if (!$companyId){
            $companyId = session('companyIds') ?? [];
        }

        return $query->whereIn('company_id', is_int($companyId) ? (array)$companyId : $companyId);
    }
}
